package br.edu.ufc.deti.util;
import com.jcraft.jsch.*;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import java.io.*;

public class Testrad {

    public static void main(String args[]) throws IOException{
        String user = "root";
        String host = "128.135.125.21";
        String cmd = "ls - l" ;
      
        JSch jsch = new JSch();
        try{
            Session session = jsch.getSession(user, host, 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("");
            UserInfo usrInfo = new MyUserInfo();
            session.setUserInfo(usrInfo);
            session.connect();
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(cmd);
          
            channel.setXForwarding(true);
            
            InputStream in = channel.getInputStream();
            channel.connect();
            channel.setInputStream(System.in);
             
            byte[] tmp = new byte[1024];
            while (true){
                while (in.available() > 0){
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0){
                        break;
                    }
                    System.out.print(new String(tmp, 0, i));
                }
                if (channel.isClosed()){
                    in.close();
                    break;
                }
                try{
                    Thread.sleep(1000);
                }
                catch (Exception ee){
                }
            }
            channel.disconnect();
            session.disconnect();
        }
        catch (Exception e) {
			e.printStackTrace();
			System.out.println("Exception" + e);
		}
    }

    public static class MyUserInfo implements UserInfo {

		public String getPassword() {
			return "";
		}

		public String getPassphrase() {
			return "";
		}

		public boolean promptPassword(String arg0) {
			return true;
		}

		public boolean promptPassphrase(String arg0) {
			return true;
		}

		public boolean promptYesNo(String arg0) {
			return true;
		}

		public void showMessage(String arg0) {
		}
    }
}
